package cn.gdcp.service01;

import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements IOnTimeChangeListener{
    private Button btnStart;
    private TextView txtTime;
    private MyReceiver receiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.e("MainActivity","thread id= "+ Thread.currentThread().getId());

        receiver=new MyReceiver(MainActivity.this);
        IntentFilter filter=new IntentFilter();
        filter.addAction("TIME_CHANGE");
        registerReceiver(receiver,filter);

        btnStart=findViewById(R.id.btn_start);
        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(MainActivity.this,MyService.class);
                startService(intent);
            }
        });
        txtTime=findViewById(R.id.txt_time);
    }

    @Override
    public void show(String str) {
        txtTime.setText(str);
    }
}
/* //第一种 MyReceiver设成内部类
class MyReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        String timeStr=intent.getStringExtra("time");
        txtTime.setText(timeStr);
    }*/
//第二种 MyReceiver设成外部类